const Koa = require("koa");
const Router = require("@koa/router");
const app = new Koa();
const router = new Router();
const { koaBody } = require("koa-body");
const OSS = require("ali-oss");

const oss = new OSS({
  region: "oss-cn-shanghai",
  accessKeyId: "LTAI5tGKjLti8icdfSVyCcSA",
  accessKeySecret: "qvqWeHID1ITkny45CFP33uNxNEWxVF",
  bucket: "2104a-1",
});

app.use(
  koaBody({
    multipart: true,
  })
);

router.post("/upload", async (ctx) => {
  const { originalFilename, filepath } = ctx.request.files.file;

  try {
    const result = await oss.put(originalFilename, filepath);

    ctx.body = {
      code: 200,
      data: result,
    };
  } catch (error){
    ctx.body = {
      code: 400,
      data: error,
    };
  }
});

app.use(router.routes()).use(router.allowedMethods());
app.listen(7001);
